/*
 * @Author: wwssaabb
 * @Date: 2021-12-10 09:22:38
 * @LastEditTime: 2021-12-10 10:26:30
 * @FilePath: \handwritten-code\main\数组去重.js
 */

/* 
数组去重
*/

let arr = [1, 1, '2', '2', true, true, false, false, Symbol(1), Symbol(1)]


//方法一：利用Set特性，元素不会重复的集合，ES6,最后集合需要转化成数组
let arr1 = [...new Set(arr)]
console.log(arr1)

//方法二: (reduce/for/filter)遍历+(includes/indexOf/findIndex)
let arr2 = arr.reduce((pre, cur) => {
  if (!pre.includes(cur)) pre.push(cur)
  return pre
}, [])
console.log(arr2)

//filter
let arr4 = arr.filter((i, index, array) => array.indexOf(i) === index)
console.log(arr4)

//方法三：利用对象key去重，reduce+object+Object.values
let arr3 = Object.values(arr.reduce((pre, cur) => {
  pre[cur] = cur
  return pre
}, {}))
console.log(arr3) //Symbol类型不可枚举